package com.group.eams.controller;






import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.SupplierPersonnelQueryDto;
import com.group.eams.pojo.entity.SupplierPersonnel;
import com.group.eams.service.SupplierPersonnelService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;




/**
 * 供应商联系人(SupplierPersonnel)表控制层
 *
 * @author 
 * @since 2024-10-30 12:58:29
 */
@RestController
@RequestMapping("/supplierPersonnel")
public class SupplierPersonnelController  {
    /**
     * 服务对象
     */
    @Autowired
    private SupplierPersonnelService supplierPersonnelService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list(SupplierPersonnelQueryDto sPersonnelQueryDto){
        Page<SupplierPersonnel> page = new Page<>(sPersonnelQueryDto.getPage(), sPersonnelQueryDto.getLimit());
        LambdaQueryWrapper<SupplierPersonnel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StrUtil.isEmpty(sPersonnelQueryDto.getName()),
                SupplierPersonnel::getName, sPersonnelQueryDto.getName())
                .like(!StrUtil.isEmpty(sPersonnelQueryDto.getPhone()),
                        SupplierPersonnel::getPhone, sPersonnelQueryDto.getPhone());

        Page<SupplierPersonnel> sPersonnelPage = supplierPersonnelService.page(page, queryWrapper);

        return Result.buildSuccess(sPersonnelPage.getTotal(), sPersonnelPage.getRecords());

    }


    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(@RequestParam Long id){
        SupplierPersonnel supplierPersonnel = supplierPersonnelService.getById(id);
        return Result.buildSuccess(supplierPersonnel);
    }


    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll() {
        return Result.buildSuccess(supplierPersonnelService.list());
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody SupplierPersonnel supplierPersonnel) {
        supplierPersonnelService.save(supplierPersonnel);
        return Result.buildSuccess();
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody SupplierPersonnel supplierPersonnel) {
        LambdaUpdateWrapper<SupplierPersonnel> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(SupplierPersonnel::getId, supplierPersonnel.getId());
        boolean result = supplierPersonnelService.update(supplierPersonnel, queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result delete(@RequestParam Long id){
        LambdaQueryWrapper<SupplierPersonnel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SupplierPersonnel::getId, id);
        boolean result = supplierPersonnelService.remove(queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/batchRemove",method = RequestMethod.POST)
    public Result batchRemove(@RequestBody BatchDeleteDto batchDeleteDto){

        LambdaQueryWrapper<SupplierPersonnel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SupplierPersonnel::getId, batchDeleteDto.getIds());
        boolean result = supplierPersonnelService.remove(queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }


}

